package com.dengqi.mymodule.activity.glide.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.load.engine.GlideException;
import com.dengqi.mymodule.R;
import com.module.imagelib.glide.ImageLoader;
import com.module.imagelib.glide.progress.CircleProgressView;
import com.module.imagelib.glide.progress.OnProgressListener;
import com.module.imagelib.glide.progress.ProgressManager;
import com.module.imagelib.glide.strategy.GlideStrategy;
import com.module.imagelib.glide.ui.GlideView;

public class GlideFragment extends PreLoadFragment {
    private static final String ARG_PARAM1 = "url";
    private static final String ARG_PARAM2 = "title";

    private String title;
    private String url;
    private CircleProgressView circleProgressView;
    private GlideView imageView;
    private TextView text;
    private boolean mIsDataInited;

    public GlideFragment() {
        // Required empty public constructor
    }

    public static GlideFragment newInstance(String url, String title) {
        GlideFragment fragment = new GlideFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, url);
        args.putString(ARG_PARAM2, title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            url = getArguments().getString(ARG_PARAM1);
            title = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_glide, container, false);
    }

    @Override
    public void initView(View view) {
        imageView = view.findViewById(R.id.glideView);
        circleProgressView = view.findViewById(R.id.circleProgressView);
        text = view.findViewById(R.id.text);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        text.setText(title);
        imageView.setBackgroundColor(Color.GRAY);
        circleProgressView.setVisibility(View.GONE);
        circleProgressView.setProgress(0);
        ProgressManager.addProgressListener(onProgressListener);
        ImageLoader.getInstance().setImageLoader(new GlideStrategy(getContext()));
        ImageLoader.getInstance().load(url).into(imageView);
    }

    OnProgressListener onProgressListener = new OnProgressListener() {
        @Override
        public void onProgress(String imageUrl, long bytesRead, long totalBytes, final boolean isDone, GlideException exception) {
            final int percent = (int) ((bytesRead * 1.0f / totalBytes) * 100.0f);
            //防止其他下载乱入
            if (imageUrl.equals(url)) {
                circleProgressView.setProgress(percent);
                circleProgressView.post(new Runnable() {
                    @Override
                    public void run() {
                        circleProgressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
                        imageView.setVisibility(isDone ? View.VISIBLE : View.GONE);
                    }
                });
            }
        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ProgressManager.removeProgressListener(onProgressListener);
        Log.e("--", title + "onDestroyView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("--", title + "onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.e("--", title + "onDetach");
    }

}
